//
//  CAVideoController.m
//  cascade-realm
//
//  Created by QSC on 20/5/24.
//

#import "CAVideoController.h"
#import "CAVideoCell.h"
@interface CAVideoController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)NSMutableArray * blockIds;
@end

@implementation CAVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navView.hidden = YES;
    
    [self setUpView];
    
    [self getVideoSource];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getVideoSource) name:BlockDidChange object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(followChange) name:FollowDidChange object:nil];
}

- (void)setUpView{
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight);
    }];
}


- (void)getVideoSource{
    
    WeakSelf
    [CAHomeUser getVideoUsers:^(NSArray * _Nonnull array) {

        weakSelf.dataArray = [NSMutableArray arrayWithArray:array];
        [weakSelf.dataArray randomObjects];
 
        [weakSelf reloadBlockUser];
        [self.collectionView reloadData];
    }];
    
    
    
    
  
}


- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(ScreenW, ScreenH);
        
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[CAVideoCell class] forCellWithReuseIdentifier:@"CAVideoCell"];
        // 去掉滚动条
        collectionView.showsHorizontalScrollIndicator = NO;
        // 分页
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        _collectionView = collectionView;
    }
    
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *) collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    CAVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CAVideoCell" forIndexPath:indexPath];
    
    cell.user = self.dataArray[indexPath.row];
    
    return cell;
}


- (void)reloadBlockUser{
    [Http postWithURL:getblockList params:@{} isShowHud:NO isLogin:NO success:^(id  _Nonnull responseObject) {
        
        NSArray * array = [CAHomeUser mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        
        self.blockIds = [NSMutableArray array];
        for (CAHomeUser * user in array) {
            [self.blockIds addObject:user.broadcasterId];
        }
        
        NSMutableArray * newDataArray = [NSMutableArray array];
        if (self.blockIds.count > 0) {
            for (CAHomeUser * user in self.dataArray) {
                if (![self.blockIds containsObject:user.userId]) {
                    [newDataArray addObject:user];
                }
            }
            self.dataArray = newDataArray;
            [self.collectionView reloadData];
        }
        
    } failure:^(id  _Nonnull errObject) {
        
    }];
}

- (void)followChange{
    [self.collectionView reloadData];
}

@end
